package org.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.smm.pojo.BookCategory;
import org.smm.pojo.BookEntry;
import org.ssm.service.BookCategerService;
import org.ssm.service.BookService;
import org.ssm.util.PageTool;


import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Resource
    BookService bookService;
    @Resource
    BookCategerService bookCategerService;

    @RequestMapping("/list")
    public String list(Model model, BookEntry bookEntry){

        int totalRecordCount=bookService.getRecordCount(bookEntry);
        PageTool pageTool=new PageTool(totalRecordCount,bookEntry.getCurrPageNo(),3);
        int startNum=pageTool.getStartRow();
        int pageSize=pageTool.getPageSize();
        bookEntry.setStarNum(startNum);
        bookEntry.setPageSize(pageSize);

        List<BookEntry> list=bookService.getBookListByPage(bookEntry);
        List<BookCategory> plist=bookCategerService.getAllBookCaTypeList();
        model.addAttribute("list",list);
        model.addAttribute("plist",plist);
        model.addAttribute("bookEntry",bookEntry);
        model.addAttribute("pageTool",pageTool);
        model.addAttribute("prePages",pageTool.getPrePages());
        model.addAttribute("nextPages",pageTool.getNextPages());
        return "/book/list";
    }

    @RequestMapping("/save")
    public String save(Model model,Integer categoryId){

        List<BookCategory> plist=bookCategerService.getAllBookCaTypeList();
        model.addAttribute("plist",plist);
        model.addAttribute("categoryId",categoryId);
        return "/book/save";
    }

    @RequestMapping("/dosave")
    public String dosave(Model model,BookEntry bookEntry) {
        System.out.println(bookEntry.getTitle());
        System.out.println(bookEntry.getCategoryId());
        System.out.println(bookEntry.getSummary());
        bookService.save(bookEntry);

        return "/book/save";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String doDelete(Model model,int id){
        try {
            bookService.delete(id);
            return "1";
        }catch (Exception e){
            return "0";
        }
    }

    @RequestMapping("/update")
    public  String update(Model model,Integer id){
        BookEntry bookEntry=bookService.getOneBook(id);

        model.addAttribute("bookEntry",bookEntry);
        return "/book/update";
    }

    @RequestMapping("/doupdate")
    public String doupdate( Model model,BookEntry bookEntry){
        if(bookService.update(bookEntry)>0){
            model.addAttribute("label","alert('修改数据成功');window.location='/book/list' ");
        }else{
            model.addAttribute("label","alert('修改数据失败');window.location='/book/update?id="+bookEntry.getId()+"';");
        }
        return  "redirect:/book/list";
    }
}

